package it.rockliveitalia;

import java.util.ArrayList;
import java.util.List;

import com.google.analytics.tracking.android.EasyTracker;

import it.rockliveitalia.database.EventDbHelper;
import it.rockliveitalia.entity.Event;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class EventListActivity extends Activity {
	
	private ListView listView=null;
	private List<Event> eventList= new ArrayList<Event>();
	private List<String> ids;
	protected static String[] eventDetails = new String[13];
	protected Context context;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_event_list);
		context = this.getApplicationContext();
		listView = (ListView) findViewById (R.id.event_list_view);
		
		Intent intent = getIntent();
		ids = intent.getStringArrayListExtra("ids");
		setTitle("Filtro: "+intent.getStringExtra("filter"));
		eventList = new ArrayList<Event>();
		EventDbHelper helper = new EventDbHelper(this);
		for(int i = 0; i<ids.size(); i++){
			Cursor c = helper.getEventById(ids.get(i));
			c.moveToFirst();
			eventList.addAll(helper.cursorToEvents(c));
		}
		
		CustomAdapterOptimize adapter = new CustomAdapterOptimize(this, R.layout.event_list_element, eventList);
        listView.setAdapter(adapter);
        
        OnItemClickListener clickListener = new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> adapter, View view,
                int position, long id) {
                Event myEvent = (Event)adapter.getItemAtPosition(position);
                eventDetails = getEventDetails(myEvent);
				Intent intent = new Intent(context,
						EventActivity.class);
				intent.putExtra("eventDetails", eventDetails);
				startActivity(intent);
            }
        };
        listView.setOnItemClickListener(clickListener);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		return true;
	}
	
	private String[] getEventDetails(Event event) {
		String[] details = new String[13];

		details[0] = String.valueOf(event.getId());
		details[1] = event.getVenueName();
		details[2] = event.getVenueSite();
		details[3] = event.getStringDate();
		details[4] = event.getStreet();
		details[5] = event.getCity();
		details[6] = event.getProvincia();
		details[7] = event.getBand();
		details[8] = event.getBandSite();
		details[9] = event.getDescription();
		details[10] = event.getImage();
		details[11] = event.getLat();
		details[12] = event.getLng();

		return details;
	}
	
	public class CustomAdapterOptimize extends ArrayAdapter<Event> {

	    public CustomAdapterOptimize(Context context, int textViewResourceId,
	                 List<Event> objects) {
	        super(context, textViewResourceId, objects);
	    }

	    @Override
	    public View getView(int position, View convertView, ViewGroup parent) {
	        return getViewOptimize(position, convertView, parent);
	    }

	    public View getViewOptimize(int position, View convertView, ViewGroup parent) {
	        ViewHolder viewHolder = null;
	        if (convertView == null) {
	            LayoutInflater inflater = (LayoutInflater) getContext()
	                      .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	            convertView = inflater.inflate(R.layout.event_list_element, null);
	            viewHolder = new ViewHolder();
	            viewHolder.date = (TextView)convertView.findViewById(R.id.element_date);
	            viewHolder.band = (TextView)convertView.findViewById(R.id.element_band);
	            viewHolder.venue = (TextView)convertView.findViewById(R.id.element_venue);
	            convertView.setTag(viewHolder);
	        } else {
	            viewHolder = (ViewHolder) convertView.getTag();
	        }
	        Event event = getItem(position);
	        viewHolder.date.setText(event.getStringDate());
	        viewHolder.band.setText(event.getBand());
	        viewHolder.venue.setText(event.getVenueName()+", "+event.getCity());
	        return convertView;
	    }

	    private class ViewHolder {
	        public TextView date;
	        public TextView band;
	        public TextView venue;
	    }
	}
	
	@Override
	protected void onStart() {
		super.onStart();
		EasyTracker.getInstance(this).activityStart(this);
	}
	
	@Override
	  public void onStop() {
	    super.onStop();
	    EasyTracker.getInstance(this).activityStop(this);
	  }

}
